/**
 * Created by litao on 12/10/2016.
 */
import java.sql.*;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016/12/9.
 */
public class DB {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet res = null;

    public DB(String dbname, String user, String pass) {
        try {
            //加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            //连接数据库
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbname + "?characterEncoding=utf8&useSSL=true", user, pass);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //查找address表，返回所有地址信息
    public ArrayList<String> searchAddress() {
        ArrayList<String> result=new ArrayList<>();
        try {
            pst = con.prepareStatement("SELECT * FROM mailaddress" );
            res = pst.executeQuery();
            while(res.next())
            {
                String address = res.getString(2);
                result.add(address);
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

}
